#include "Message.h"




Message::Message() {}


Message::Message(std::string to) :
	to(to)
{}


Message::Message(std::string to, TYPE type) :
	to(to),
	type(type)
{}


std::string Message::getBody(std::string language) {
	language = parseXMLLang(language);
	for(std::list<Body*>::iterator it = bodies.begin();
			it != bodies.end(); it ++) {
		if( ((*it)->getLanguage().empty() && language.empty()) 
				|| ( (*it) != NULL && (*it)->getLanguage() == language)) {
			return (*it)->getLanguage();
		}
	}
	return "";
}


std::list<Body*> Message::getBodies() { 
	return bodies;
}


void Message::setBody(std::string & body) {
	if (body.empty()) {
		removeBody("");
		return;
	}
	addBody(std::string(""), body);
}


Body * Message::addBody(std::string & language, std::string & body) {
	if (body.empty) {
		return NULL;
	}
	language = parseXMLLang(language);

	Body * messageBody = new Body(language, body);
	bodies.add(messageBody);
	return messageBody;
}


void Message::removeBody(std::string language) {
	language = parseXMLLang(language);

	for(std::list<Body*>::iterator it = bodies.begin(); 
			it != bodies.end(); it ++) {
		if( language == (*it)->getLanguage()) {
			Body * b = *it;
			delete b;
			bodies.remove(*it);
		}
	}
}


void Message::removeBody(Body * body) {
   bodies.remove(body);
   delete body;
}


std::list<std::string> Message::getBodyLanguages() {
	std::list<std::string> languages;
	for(std::list<Body*>::iterator it = bodies.begin(); 
			it != bodies.end(); it ++) {
		if (!parseXMLLang((*it)->getLangauge()) == getDefaultLanguage()) {
			languages.add((*it)->getLangauge());
		}
	}
}


std::string Message::getThread() {
    return thread;
}


void Message::setThread(std::string & thread) {
	this->thread = thread;
}


std::string Message::getLanguage() {
    return language;
}


void Message::setLanguage(std::string language) {
    this->language = language;
}


std::string Message::toXML() {
	std::string buf("<message") 
	if (!getXmlns().empty()) {
		buf.append(" xmlns=\"").append(getXmlns()).append("\"");
	}
	if (!language.empty()) {
		buf.append(" xml:lang=\"").append(language).append("\"");
	}
	if (!getPacketID().empty()) {
		buf.append(" id=\"").append(getPacketID()).append("\"");
	}
	if (!getTo().empty()) {
		buf.append(" to=\"").append(StringUtils::escapeForXML(getTo())).append("\"");
	}
	if (!getFrom().empty()) {
		buf.append(" from=\"").append(StringUtils::escapeForXML(getFrom())).append("\"");
	}
	if (type != normal) {
		buf.append(" type=\"").append(type).append("\"");
	}
	buf.append(">");
	if (!subject.empty()) {
		buf.append("<subject>").append(StringUtils::escapeForXML(subject)).append("</subject>");
	}
	// Add the body in the default language
	if (!getBody().empty()) {
		buf.append("<body>").append(StringUtils::escapeForXML(getBody())).append("</body>");
	}
	// Add the bodies in other languages
	for(std::list<Body*>::iterator it = bodies.begin(); 
			it != bodies.end(); it ++) { 
		if (DEFAULT_LANGUAGE == (*it)->getLanguage() || (*it)->getLanguage().empty()) {
			continue;
		}
		buf.append("<body xml:lang=\"").append((*it)->getLanguage()).append("\">");
		buf.append(StringUtils::escapeForXML((*it)->getMessage()));
		buf.append("</body>");
	}
	if (!thread.empty()) {
		buf.append("<thread>").append(thread).append("</thread>");
	}
	// Append the error subpacket if the message type is an error.
	if (type == error) {
		XMPPError * error = getError();
		if (error != NULL) {
			buf.append(error->toXML());
		}
	}
	// Add packet extensions, if any are defined.
	buf.append(getExtensionsXML());
	buf.append("</message>");
	return buf;
}


int Message::hashCode() {
	//TODO
	return 0;
}
